package com.way.getWeather;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class ChinaWeather {
	public static Map<String,String> getWeather(){
		try {
			URL url = new URL("http://m.weather.com.cn/data/101280601.html");
			InputStream is = url.openStream();
			System.out.println(is.available());
			byte[] buf = new byte[is.available()];
			is.read(buf);
//			System.out.println(new String(buf, "utf-8"));
			String all = new String(buf, "utf-8");
			String subAll = all.substring(all.lastIndexOf("{") + 1,
					all.indexOf("}"));
//			System.out.println(subAll);
			String[] strs = new String[] {};
			strs = subAll.split(",");
			Map<String, String> map = new HashMap<String, String>();
			for (String str : strs) {
//				System.out.println(str);
				String[] subStrs = new String[] {};
				subStrs = str.split(":");
				String key = subStrs[0].substring(subStrs[0].indexOf("\"")+1,subStrs[0].lastIndexOf("\""));
				String values = subStrs[1].substring(subStrs[1].indexOf("\"")+1,subStrs[1].lastIndexOf("\""));
				map.put(key, values);
			}
			System.out.println(map.get("city"));
//			System.out.println(map.toString());
			return map;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
